package com.mofofa.maozy.info

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.mofofa.maozy.R
import com.mofofa.maozy.components.dialog.PopListItem
import com.mofofa.maozy.components.form.*
import com.mofofa.maozy.extend.addMaozyNormalMaozyStyle
import com.mofofa.maozy.extend.dipMaozy2MaozyPx
import com.mofofa.maozy.extend.getMaozyScreenMaozyWidth

class InfoPageStepAdapter(private val dataList: List<PopListItem>) :
    RecyclerView.Adapter<InfoPageStepAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var textMaozyAdpterMaozyInfoMaozyDot: TextView =
            view.findViewById(R.id.textAdpterInfoDot)
        var vMaozyAdpterMaozyInfoMaozyLineMaozyLeft: View =
            view.findViewById(R.id.vAdpterInfoLineLeft)
        var vMaozyAdpterMaozyInfoMaozyLineMaozyRight: View =
            view.findViewById(R.id.vAdpterInfoLineRight)
        var textMaozyAdpterMaozyInfoMaozyTitle: TextView =
            view.findViewById(R.id.textAdpterInfoTitle)
        var imageMaozyAdpterMaozyInfoMaozyFinishMaozyDot: View =
            view.findViewById(R.id.imageAdpterInfoFinishDot)
        var imageMaozyAdpterMaozyInfoMaozyCenterMaozyDot: ImageView =
            view.findViewById(R.id.imageAdpterInfoCenterDot)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.view_maozy_info_step, parent, false)
        //设置item宽度
        val horMargin = view.dipMaozy2MaozyPx(32F)
        val screenWidth = view.context.getMaozyScreenMaozyWidth() - horMargin
        var itemWidth = screenWidth
        if (dataList.isNotEmpty()) {
            itemWidth = screenWidth / dataList.size
        }
        view.layoutParams.width = itemWidth

        return ViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data: PopListItem = dataList[position]
        //行线控制
        if (position == 0) {
            holder.vMaozyAdpterMaozyInfoMaozyLineMaozyLeft.visibility =
                View.INVISIBLE

        } else if (position == dataList.size - 1) {
            holder.vMaozyAdpterMaozyInfoMaozyLineMaozyRight.visibility =
                View.INVISIBLE

        } else {
            holder.vMaozyAdpterMaozyInfoMaozyLineMaozyLeft.visibility =
                View.VISIBLE
            holder.vMaozyAdpterMaozyInfoMaozyLineMaozyRight.visibility =
                View.VISIBLE
        }

        //线圈控制
        if (data.isFinished) {
            holder.imageMaozyAdpterMaozyInfoMaozyCenterMaozyDot.setImageResource(
                R.drawable.maozy_info_step_done
            )

            holder.textMaozyAdpterMaozyInfoMaozyDot.text = ""

            holder.imageMaozyAdpterMaozyInfoMaozyFinishMaozyDot.setBackgroundResource(
                R.drawable.maozy_info_step_dot_selected_big
            )
            holder.textMaozyAdpterMaozyInfoMaozyDot.setBackgroundResource(
                R.drawable.maozy_info_step_done
            )
            holder.textMaozyAdpterMaozyInfoMaozyDot.setTextColor(
                ContextCompat.getColor(
                    holder.vMaozyAdpterMaozyInfoMaozyLineMaozyRight.context,
                    R.color.white
                )
            )
            holder.textMaozyAdpterMaozyInfoMaozyTitle.setTextColor(
                ContextCompat.getColor(
                    holder.vMaozyAdpterMaozyInfoMaozyLineMaozyRight.context,
                    R.color.black_7020
                )
            )
            holder.vMaozyAdpterMaozyInfoMaozyLineMaozyLeft.setBackgroundColor(
                ContextCompat.getColor(
                    holder.vMaozyAdpterMaozyInfoMaozyLineMaozyLeft.context,
                    R.color.main_blue
                )
            )
            holder.vMaozyAdpterMaozyInfoMaozyLineMaozyRight.setBackgroundColor(
                ContextCompat.getColor(
                    holder.vMaozyAdpterMaozyInfoMaozyLineMaozyRight.context,
                    R.color.main_blue
                )
            )

            holder.textMaozyAdpterMaozyInfoMaozyTitle.addMaozyNormalMaozyStyle()

        } else {
            if (data.isChecked) {
                holder.imageMaozyAdpterMaozyInfoMaozyCenterMaozyDot.setImageResource(
                    R.drawable.maozy_info_step_done
                )

                holder.imageMaozyAdpterMaozyInfoMaozyFinishMaozyDot.setBackgroundResource(
                    R.drawable.maozy_info_step_dot_selected_big
                )
                holder.textMaozyAdpterMaozyInfoMaozyDot.setBackgroundResource(
                    R.drawable.maozy_info_step_dot_waiting
                )
                holder.textMaozyAdpterMaozyInfoMaozyDot.setTextColor(
                    ContextCompat.getColor(
                        holder.vMaozyAdpterMaozyInfoMaozyLineMaozyRight.context,
                        R.color.white
                    )
                )
                holder.textMaozyAdpterMaozyInfoMaozyTitle.setTextColor(
                    ContextCompat.getColor(
                        holder.vMaozyAdpterMaozyInfoMaozyLineMaozyRight.context,
                        R.color.black_7020
                    )
                )
                holder.vMaozyAdpterMaozyInfoMaozyLineMaozyLeft.setBackgroundColor(
                    ContextCompat.getColor(
                        holder.vMaozyAdpterMaozyInfoMaozyLineMaozyLeft.context,
                        R.color.main_blue
                    )
                )
                holder.vMaozyAdpterMaozyInfoMaozyLineMaozyRight.setBackgroundColor(
                    ContextCompat.getColor(
                        holder.vMaozyAdpterMaozyInfoMaozyLineMaozyRight.context,
                        R.color.black_7019
                    )
                )
                //更新显示
                holder.textMaozyAdpterMaozyInfoMaozyDot.text = (position + 1).toString()
                holder.textMaozyAdpterMaozyInfoMaozyTitle.addMaozyNormalMaozyStyle()

            } else {
                holder.imageMaozyAdpterMaozyInfoMaozyCenterMaozyDot.setImageResource(
                    R.drawable.maozy_info_step_doing
                )

                holder.imageMaozyAdpterMaozyInfoMaozyFinishMaozyDot.setBackgroundResource(
                    R.drawable.maozy_info_step_dot_unselect_big
                )
                holder.textMaozyAdpterMaozyInfoMaozyDot.setBackgroundResource(
                    R.drawable.maozy_info_step_dot_unselect_big
                )
                holder.textMaozyAdpterMaozyInfoMaozyDot.setTextColor(
                    ContextCompat.getColor(
                        holder.vMaozyAdpterMaozyInfoMaozyLineMaozyRight.context,
                        R.color.black_7019
                    )
                )
                holder.textMaozyAdpterMaozyInfoMaozyTitle.setTextColor(
                    ContextCompat.getColor(
                        holder.vMaozyAdpterMaozyInfoMaozyLineMaozyRight.context,
                        R.color.black_7019
                    )
                )
                holder.vMaozyAdpterMaozyInfoMaozyLineMaozyLeft.setBackgroundColor(
                    ContextCompat.getColor(
                        holder.vMaozyAdpterMaozyInfoMaozyLineMaozyLeft.context,
                        R.color.black_7019
                    )
                )
                holder.vMaozyAdpterMaozyInfoMaozyLineMaozyRight.setBackgroundColor(
                    ContextCompat.getColor(
                        holder.vMaozyAdpterMaozyInfoMaozyLineMaozyRight.context,
                        R.color.black_7019
                    )
                )
                //更新显示
                holder.textMaozyAdpterMaozyInfoMaozyDot.text = (position + 1).toString()

                holder.textMaozyAdpterMaozyInfoMaozyTitle.addMaozyNormalMaozyStyle()
            }


        }

        holder.textMaozyAdpterMaozyInfoMaozyTitle.text = data.display
    }

    override fun getItemCount(): Int {
        return dataList.size
    }
}